<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/16
 * Time: 16:20
 */

namespace app\manage\controller;

use app\manage\model\User;
use app\manage\model\Token;
use think\Db;
use think\Request;
use think\Validate;
use think\captcha\Captcha;

class Login
{

    public function __construct(){
        $this->token=new Token();
        $this->user=new User();
    }

    /**
     * 登录
     */
    public function login()
    {

        $param=input();
        $validate = new Validate([
            ['account', 'require', '账号不能为空'],
            ['password', 'require', '密码不能为空'],
//            ['code', 'require', '验证码不能为空'],
//            ['sid', 'require', 'sid不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $captcha=new Captcha;
        //$sid='1594799421';
//        $sid=$param['sid'];

//         if(!captcha_check($param['code'])){
//             returnJson(-2003,'验证码错误');
//         }

//        if(!$captcha->check($param['code'],$sid)){
//            returnJson(-2003,'验证码错误');
//        }

        $user_id=$this->user->doLogin($param);
        //查看用户头像和昵称
        $user=$this->user->selectOne($user_id);

        #根据后台登录用户id生成token
        $returnData['token']=$this->token->createToken($user_id);
        $returnData['user_id']=$user_id;
        $returnData['avatar']=$user['avatar'];
        $returnData['nickname']=$user['user_nickname'];

        returnJson(200,'success',$returnData);

    }

    /**
     * 退出登录
     */
    public function logout(){
        $user_id=get_header_id('token');
        //$user_id=1;
        $file_dir = ROOT_PATH.'public'.DS.'manage_token'.DS.$user_id.DS;
        //dump($file_dir);
        $file_token = scandir( $file_dir );
        //dump($file_token);
        foreach ( $file_token as $k => $v ) {
            if($v == '.' || $v == '..'){}else {
                //dump($file_dir.$v);
                unlink($file_dir.$v);
            }
        }

        returnJson(200,'操作成功');

    }

    /**
     * 验证码接口
     */

    public function getverify(){
//        session_start();
//        $sid=session_id();#获取单签sessionid
        $time=time();
        $url=$_SERVER['HTTP_HOST'];
        //$data['captcha_url']="http://".$url."/index.php/manage/login/verify?sid=".$time;
        $data['captcha_url']="https://".$url."/index.php/manage/login/verify?sid=".$time;
        $data['sid']=$time;

        returnJson(200,'success',$data);
    }

    // 生成验证码
    public function verify(){

        $sid=request()->get('sid');
//        session_id($sid); #指定sessionid
//        session_start();#开启session
        $config=[
            // 验证码字符集合
            'codeSet'=> '1234567890',
            // 验证码字体大小(px)
            'fontSize'=> 15,
            // 是否画混淆曲线
            'useCurve'=> false,
            // 验证码图片高度
            'imageH'=> 30,
            // 验证码图片宽度
            'imageW'=> 140,
            // 验证码位数
            'length'=> 4,
            // 验证成功后是否重置        
            'reset'=> true
        ];
        $captcha=new Captcha($config);
        return $captcha->entry(); #传入session标识

    }

    #zlc-查询验证码数字
    public function findCodeNun()
    {
        $ip = Request::instance()->ip();
        $code = Db::name("admin_verify")
            ->field("code_num")
            ->where(['ip'=>$ip])
            ->order(['id'=>"desc"])
            ->find();
        returnJson(200,"success",$code);

    }

    //添加后台用户
    public function test(){
        $pass='123456';
        $data['user_login']='test123';
        $data['user_pass']=cmf_password($pass);
        $data['create_time']=time();

        $user_id=Db::name('user')->insertGetId($data);
    }

    public function check(){
        //dump('testhahaha');
        $pass='123456';
        $p2=md5($pass);
        $authCode = 'Dex9gPtaHut5nzobzV';

        $p6= cmf_password($p2);
        $p7='###c6e68747d36c9e3d8ef631eb50e5500e';

        dump($p2);
        dump($p6);
        dump(cmf_compare_password($p2,$p7));
    }
    //权限测试
    public function manage(){
        dump('权限测试');
    }


}